﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using log4net.Config;
using TSUnion.KMIP.API;
using TSUnion.KMIP.Communication;
using TSUnion.KMIP.Communication.Transport;
using TSUnion.KMIP.Core;
using TSUnion.KMIP.Core.Enumerators;
using TSUnion.KMIP.Core.KeyMaterials;
using TSUnion.KMIP.Core.Request;
using TSUnion.KMIP.Core.Response;

namespace TSUnion.KMIP.TestClient
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlConfigurator.Configure();

            Thread.Sleep(2000);

            //ConnectionDetails details = ConnectionDetails.CreateConnectionFromConfig();
            //IClientTransport client = TransportFactory.CreateClientChannel(details.Transport);

            //client.Connect(details);


            //RequestMessage request = OperationFactory.Create(KeyCryptographicAlgorithmType.DES3, new CryptographicUsageMask() { DeriveKey = true }, ObjectType.SymmetricKey);
            //client.SendRequest(request);
            //ResponseMessage responseCreate = client.WaitForResponse();

            //Guid keyId = responseCreate.GetFirstReturnedId();


            //RequestMessage requestGet = OperationFactory.Get(keyId);
            //client.SendRequest(requestGet);
            //ResponseMessage responseGet = client.WaitForResponse();

            //BaseObject obj = null;
            //if (responseGet.IsIncludeManagedObject())
            //{
            //    obj = responseGet.GetIncludedManagedObject();
            //}


            //KeyBlock<SymmetricKeyMaterial> block = KeyMaterialConverter<KeyBlock<SymmetricKeyMaterial>>.ConvertFrom(obj.Key);


            //string cryptoText = Crypto.EncryptStringAES("hello", block.Value.Instance.Key, block.Salt, block.Iterations);
            //string normalText = Crypto.DecryptStringAES(cryptoText, block.Value.Instance.Key, block.Salt, block.Iterations);
            //Console.WriteLine("Decoded value: " + normalText);
            //client.Close();

            string attrName = "PersonOrderKey";
            string attrValue = @"http:\\331\34\3\5\7\5\4\4";
            IClientTransport client = TransportFactory.CreateClientChannel(TrasportType.TCP);
            ConnectionDetails details = ConnectionDetails.CreateConnectionFromConfig();
            client.Connect(details);

            RequestMessage request = OperationFactory.Create(KeyCryptographicAlgorithmType.AES, new Core.CryptographicUsageMask() { DeriveKey = true }, Core.Enumerators.ObjectType.SymmetricKey);
            client.SendRequest(request);
            ResponseMessage response = client.WaitForResponse();

            request = OperationFactory.AddAttribute(response.GetFirstReturnedId(), attrName, attrValue);
            client.SendRequest(request);
            response = client.WaitForResponse();


            request = OperationFactory.ModifyAttribute(response.GetFirstReturnedId(), attrName, attrValue);
            client.SendRequest(request);
            response = client.WaitForResponse();

            client.Close();

            Console.ReadKey();

        }
    }
}
